package yxy.game.pm2.module.active.progress;

import java.util.Map;

import org.joda.time.DateTime;

import yxy.apple.util.PubSub.MethodSubscriber;
import yxy.apple.util.PubSub.SubscriptionMethod;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.rankschedule;
import yxy.game.pm2.bean.pubsub.ConquestPublisher;
import yxy.game.pm2.bean.pubsub.PlayerBeanPublisher;
import yxy.game.pm2.bean.pubsub.TowerPublisher;
import yxy.game.pm2.bean.pubsub.ConquestPublisher.ConquestSubscription;
import yxy.game.pm2.bean.pubsub.PlayerBeanPublisher.PlayerBeanSubscription;
import yxy.game.pm2.bean.pubsub.TowerPublisher.TowerSubscription;

public class RankProgressSubscriber extends MethodSubscriber {

	static private RankProgressSubscriber instance;

	public static RankProgressSubscriber getInstance() {
		if (instance == null) {
			instance = new RankProgressSubscriber();
			instance.init();
		}
		return instance;
	}

	private void init() {
		ConquestPublisher.getInstance().subscribe(this);
		PlayerBeanPublisher.getInstance().subscribe(this);
		TowerPublisher.getInstance().subscribe(this);
	}
	
	private RankProgressSubscriber() {
	}

	@SubscriptionMethod(Type = ConquestSubscription.class)
	public void receive(ConquestSubscription subscription) {
		String playercode = subscription.getPlayerCode();
		int mapid = subscription.getMapId();
		
		Map<String, rankschedule> rsmap = DataCenter.getAllData(rankschedule.class);
		RankProgressArea areamodule = RankProgressArea.get(playercode, RankProgressArea.ConquestRank);
		
		try {
			if(areamodule.tryLock()) {
				boolean save = false;
				for(rankschedule rs : rsmap.values()) {
					if(rs.getType() != areamodule.getType()) {
						continue;
					}
					
					if(mapid < rs.getCondition()) {
						continue;
					}
					
					if(!areamodule.getRpmap().containsKey(rs.getId())) {
						RankProgress rp = new RankProgress();
						rp.setPlayercode(playercode);
						rp.setTime(DateTime.now());
						areamodule.getRpmap().put(rs.getId(), rp);
						save = true;
					}
					
					if(save) areamodule.set();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			areamodule.unlock();
		}
	}
	
	@SubscriptionMethod(Type = PlayerBeanSubscription.class)
	public void receive(PlayerBeanSubscription subscription) {
		String playercode = subscription.getPlayerBean().getCode();
		long power = subscription.getPlayerBean().getPower();
		
		Map<String, rankschedule> rsmap = DataCenter.getAllData(rankschedule.class);
		RankProgressArea areamodule = RankProgressArea.get(playercode, RankProgressArea.PowerRank);
		
		try {
			if(areamodule.tryLock()) {
				boolean save = false;
				for(rankschedule rs : rsmap.values()) {
					if(rs.getType() != areamodule.getType()) {
						continue;
					}
					
					if(power < rs.getCondition()) {
						continue;
					}
					
					if(!areamodule.getRpmap().containsKey(rs.getId())) {
						RankProgress rp = new RankProgress();
						rp.setPlayercode(playercode);
						rp.setTime(DateTime.now());
						areamodule.getRpmap().put(rs.getId(), rp);
						save = true;
					}
					
					if(save) areamodule.set();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			areamodule.unlock();
		}
	}
	
	@SubscriptionMethod(Type = TowerSubscription.class)
	public void receive(TowerSubscription subscription) {
		String playercode = subscription.getPlayerCode();
		int floor = subscription.getFloor();
		
		Map<String, rankschedule> rsmap = DataCenter.getAllData(rankschedule.class);
		RankProgressArea areamodule = RankProgressArea.get(playercode, RankProgressArea.TowerRank);
		
		try {
			if(areamodule.tryLock()) {
				boolean save = false;
				for(rankschedule rs : rsmap.values()) {
					if(rs.getType() != areamodule.getType()) {
						continue;
					}
					
					if(floor < rs.getCondition()) {
						continue;
					}
					
					if(!areamodule.getRpmap().containsKey(rs.getId())) {
						RankProgress rp = new RankProgress();
						rp.setPlayercode(playercode);
						rp.setTime(DateTime.now());
						areamodule.getRpmap().put(rs.getId(), rp);		
						save = true;
					}
					
					if(save) areamodule.set();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			areamodule.unlock();
		}
	}
	
}
